$(function () {

    iniList();
    bindClick();

})

function iniList() {
    $("#userList").datagrid({
        url:'/admin/user/query',
        method:'post',
        pagination:true,
        rownumbers: true,
        local:"zh_CN",
        pageNumber:1,
        pageSize:10,
        toolbar:'#userBtn',
        singleSelect:true,
        queryParams: {
        },
        columns:[[
            {field:'userid',title:'用户id',width:50},
            {field:'username',title:'用户名',width:100},
            {field:'sex',title:'性别',width:100,
                formatter: function(value,row,index){
                    if (value == 0){
                        return "女";
                    } else if(value == 1){
                        return "男";
                    }else {
                        return "";
                    }
                }
            },
            {field:'account',title:'账号',width:100},
            {field:'mobile',title:'手机号',width:100},
            {field:'createTime',title:'创建时间',width:150,
                formatter: function(value,row,index){
                   return Kit.simpleFmtDate(value,"yyyy-MM-dd hh:mm:ss");
                }
            },
            {field:'updateTime',title:'上次更新时间',width:150,
                formatter: function(value,row,index){
                    return Kit.simpleFmtDate(value,"yyyy-MM-dd hh:mm:ss");
                }
            }

        ]],
        loadFilter: function(data){
            if (data.code == '0000'){
                return data.retData.users;
            } else {
                return [];
            }
        },
        onBeforeLoad: function (param) {

        }
    });
}

function bindClick() {
    $("#addBtn").on("click",function (){
        $('#win').window({
            width:600,
            height:400,
            modal:true,
            title:"用户增加",
            href:"/view/biz/user/addUser.html"
        });
    })

    $("#editBtn").on("click",function (){
        $('#win').window({
            width:600,
            height:400,
            modal:true,
            title:"用户编辑",
            href:"/view/biz/user/editUser.html"
        });
    })

    $("#delBtn").on("click",function (){

        $.messager.confirm('删除确认','确认是否删除',function (r){
            if(r) {
                var selectedObj = $("#userList").datagrid("getSelected");
                if(Kit.isEmpty(selectedObj)) {
                    $.messager.alert("帮助提示","请选中要删除的行");
                    return false;
                }

                ui.request({
                    url:"/admin/user/delete",
                    data:{"userid":selectedObj.userid},
                    success:function (rtn) {
                        if(rtn.code !== "0000"){
                            $.messager.alert(rtn.msg,rtn.desc);
                        }
                        $("#userList").datagrid("reload",{});
                    }
                })
            }
        });
    })
}

